# yue.nicholas@gmail.com
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT ( ilmbase )

option(ILMIMF_BUILD_TESTS         "Build IlmBase tests?" OFF)
option(ILMIMF_CREATE_LIBTOOL_FILE "Create libtool files?" OFF)
option(ILMIMF_INSTALL_PKGCONFIG   "Install pkgconfig files?" OFF)

SET(ILMBASE_VERSION_MAJOR "2")
SET(ILMBASE_VERSION_MINOR "2")
SET(ILMBASE_VERSION_PATCH "0")

SET(ILMBASE_VERSION ${ILMBASE_VERSION_MAJOR}.${ILMBASE_VERSION_MINOR}.${ILMBASE_VERSION_PATCH})
SET(ILMBASE_VERSION_API ${ILMBASE_VERSION_MAJOR}_${ILMBASE_VERSION_MINOR})

if(ILMIMF_BUILD_TESTS)
ENABLE_TESTING()
endif()

SET(CPACK_PACKAGE_VERSION_MAJOR "${ILMBASE_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${ILMBASE_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${ILMBASE_VERSION_PATCH}")
SET(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
    "${CMAKE_PROJECT_NAME}-${ILMBASE_VERSION}"
  )
set(CPACK_SOURCE_IGNORE_FILES
  "/.git*;/.cvs*;${CPACK_SOURCE_IGNORE_FILES}")
INCLUDE ( CPack )


# Allow the developer to select if Dynamic or Static libraries are built
OPTION (ILMBASE_BUILD_SHARED_LIBS "Build Shared Libraries" ON)

# Allow the developer to select if Dynamic or Static libraries are built
OPTION (ILMBASE_NAMESPACE_VERSIONING "Namespace Versioning" ON)

# Setup osx rpathing
SET (CMAKE_MACOSX_RPATH 1)
SET (BUILD_WITH_INSTALL_RPATH 1)

IF ( NOT WIN32)
  ADD_DEFINITIONS ( -pthread )
ENDIF ()

INCLUDE_DIRECTORIES ( Iex IexMath Imath Half ${CMAKE_CURRENT_BINARY_DIR}/config IlmThread)

if(ILMIMF_BUILD_TESTS)
INCLUDE_DIRECTORIES ( IexTest ImathTest HalfTest )
endif()

# also add the current directory to pick up the autogenerated headers
SET(CMAKE_INCLUDE_CURRENT_DIR ON)

MACRO(GET_TARGET_PROPERTY_WITH_DEFAULT _variable _target _property _default_value)
   GET_TARGET_PROPERTY (${_variable} ${_target} ${_property})
   IF (${_variable} MATCHES NOTFOUND)
     SET (${_variable} ${_default_value})
   ENDIF (${_variable} MATCHES NOTFOUND)
 ENDMACRO (GET_TARGET_PROPERTY_WITH_DEFAULT)

 MACRO(CREATE_LIBTOOL_FILE _target _install_DIR)
   GET_TARGET_PROPERTY(_target_location ${_target} LOCATION)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_static_lib ${_target} STATIC_LIB "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dependency_libs ${_target} LT_DEPENDENCY_LIBS "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_current ${_target} LT_VERSION_CURRENT 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_age ${_target} LT_VERSION_AGE 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_revision ${_target} LT_VERSION_REVISION 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_installed ${_target} LT_INSTALLED yes)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_shouldnotlink ${_target} LT_SHOULDNOTLINK yes)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlopen ${_target} LT_DLOPEN "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlpreopen ${_target} LT_DLPREOPEN "")
   GET_FILENAME_COMPONENT(_laname ${_target_location} NAME_WE)
   GET_FILENAME_COMPONENT(_soname ${_target_location} NAME)
   SET(_laname ${PROJECT_BINARY_DIR}/${_laname}.la)
   FILE(WRITE ${_laname} "# ${_laname} - a libtool library file\n")
   FILE(APPEND ${_laname} "# Generated by CMake ${CMAKE_VERSION} (like GNU libtool)\n")
   FILE(APPEND ${_laname} "\n# Please DO NOT delete this file!\n# It is necessary for linking the library with libtool.\n\n" )
   FILE(APPEND ${_laname} "# The name that we can dlopen(3).\n")
   FILE(APPEND ${_laname} "dlname='${_soname}'\n\n")
   FILE(APPEND ${_laname} "# Names of this library.\n")
   FILE(APPEND ${_laname} "library_names='${_soname}.${_target_current}.${_target_age}.${_target_revision} ${_soname}.${_target_current} ${_soname}'\n\n")
   FILE(APPEND ${_laname} "# The name of the static archive.\n")
   FILE(APPEND ${_laname} "old_library='${_target_static_lib}'\n\n")
   FILE(APPEND ${_laname} "# Libraries that this one depends upon.\n")
   FILE(APPEND ${_laname} "dependency_libs='${_target_dependency_libs}'\n\n")
   FILE(APPEND ${_laname} "# Names of additional weak libraries provided by this library\n")
   FILE(APPEND ${_laname} "weak_library_names=\n\n")
   FILE(APPEND ${_laname} "# Version information for ${_laname}.\n")
   FILE(APPEND ${_laname} "current=${_target_current}\n")
   FILE(APPEND ${_laname} "age=${_target_age}\n")
   FILE(APPEND ${_laname} "revision=${_target_revision}\n\n")
   FILE(APPEND ${_laname} "# Is this an already installed library?\n")
   FILE(APPEND ${_laname} "installed=${_target_installed}\n\n")
   FILE(APPEND ${_laname} "# Should we warn about portability when linking against -modules?\n")
   FILE(APPEND ${_laname} "shouldnotlink=${_target_shouldnotlink}\n\n")
   FILE(APPEND ${_laname} "# Files to dlopen/dlpreopen\n")
   FILE(APPEND ${_laname} "dlopen='${_target_dlopen}'\n")
   FILE(APPEND ${_laname} "dlpreopen='${_target_dlpreopen}'\n\n")
   FILE(APPEND ${_laname} "# Directory that this library needs to be installed in:\n")
   FILE(APPEND ${_laname} "libdir='${CMAKE_INSTALL_PREFIX}${_install_DIR}'\n")
   INSTALL( FILES ${_laname} DESTINATION ${CMAKE_INSTALL_PREFIX}${_install_DIR})
 ENDMACRO(CREATE_LIBTOOL_FILE)

SET (LIB_TYPE STATIC)
IF (ILMBASE_BUILD_SHARED_LIBS)
  # User wants to build Dynamic Libraries, so change the LIB_TYPE variable to CMake keyword 'SHARED'
  SET (LIB_TYPE SHARED)
  IF (WIN32)
    ADD_DEFINITIONS(-DOPENEXR_DLL)
  ENDIF ()
ENDIF (ILMBASE_BUILD_SHARED_LIBS)


ADD_SUBDIRECTORY ( Half )
ADD_SUBDIRECTORY ( Iex )
ADD_SUBDIRECTORY ( IexMath )
ADD_SUBDIRECTORY ( Imath )
ADD_SUBDIRECTORY ( IlmThread )

IF (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h)
  IF (WIN32)
    FILE ( COPY ${CMAKE_CURRENT_SOURCE_DIR}/config.windows/IlmBaseConfig.h
      DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/config
      )
  ELSE ()
    IF (APPLE)
      FILE ( WRITE  ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define HAVE_PTHREAD 1\n" )
    ELSE ()
      FILE ( WRITE  ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define HAVE_PTHREAD 1\n" )
      FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_HAVE_LARGE_STACK  1\n" )
      FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define HAVE_POSIX_SEMAPHORES 1\n" )
      FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_HAVE_CONTROL_REGISTER_SUPPORT 1\n")
    ENDIF ()
  ENDIF ()

  IF (ILMBASE_NAMESPACE_VERSIONING)
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_INTERNAL_ILMBASE_NAMESPACE_CUSTOM 1\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IMATH_INTERNAL_NAMESPACE Imath_${ILMBASE_VERSION_API}\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IEX_INTERNAL_NAMESPACE Iex_${ILMBASE_VERSION_API}\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMTHREAD_INTERNAL_NAMESPACE IlmThread_${ILMBASE_VERSION_API}\n")
  ELSE ()
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_INTERNAL_ILMBASE_NAMESPACE_CUSTOM 0\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IMATH_INTERNAL_NAMESPACE Imath\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IEX_INTERNAL_NAMESPACE Iex\n")
    FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMTHREAD_INTERNAL_NAMESPACE IlmThread\n")
  ENDIF ()

  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IMATH_NAMESPACE Imath\n")
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define IEX_NAMESPACE Iex\n")
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMTHREAD_NAMESPACE IlmThread\n")
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_VERSION_STRING \"${ILMBASE_VERSION}\"\n" )
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_PACKAGE_STRING \"IlmBase ${ILMBASE_VERSION}\"\n" )
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_VERSION_MAJOR ${ILMBASE_VERSION_MAJOR}\n" )
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_VERSION_MINOR ${ILMBASE_VERSION_MINOR}\n" )
  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "#define ILMBASE_VERSION_PATCH ${ILMBASE_VERSION_PATCH}\n" )

  FILE ( APPEND ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h "
// Version as a single hex number, e.g. 0x01000300 == 1.0.3
#define ILMBASE_VERSION_HEX ((ILMBASE_VERSION_MAJOR << 24) | \\
                             (ILMBASE_VERSION_MINOR << 16) | \\
                             (ILMBASE_VERSION_PATCH <<  8))
")
ENDIF()

SET (ILMBASE_LIBSUFFIX "")
IF (ILMBASE_NAMESPACE_VERSIONING)
  SET ( ILMBASE_LIBSUFFIX "-${ILMBASE_VERSION_API}" )
ENDIF ()

SET_TARGET_PROPERTIES ( Iex
  PROPERTIES
  OUTPUT_NAME "Iex${ILMBASE_LIBSUFFIX}"
  )
SET_TARGET_PROPERTIES ( Imath
  PROPERTIES
  OUTPUT_NAME "Imath${ILMBASE_LIBSUFFIX}"
  )
SET_TARGET_PROPERTIES ( IlmThread
  PROPERTIES
  OUTPUT_NAME "IlmThread${ILMBASE_LIBSUFFIX}"
  )
SET_TARGET_PROPERTIES ( IexMath
  PROPERTIES
  OUTPUT_NAME "IexMath${ILMBASE_LIBSUFFIX}"
  )

IF ( NOT WIN32 AND ILMIMF_CREATE_LIBTOOL_FILE)
  CREATE_LIBTOOL_FILE ( Half /lib )
  CREATE_LIBTOOL_FILE ( Iex /lib )
  CREATE_LIBTOOL_FILE ( IexMath /lib )
  CREATE_LIBTOOL_FILE ( Imath /lib )
  CREATE_LIBTOOL_FILE ( IlmThread /lib )
ENDIF ()

# Tests

if(ILMIMF_BUILD_TESTS)
ADD_SUBDIRECTORY ( HalfTest )
ADD_SUBDIRECTORY ( IexTest )
ADD_SUBDIRECTORY ( ImathTest )
endif()

# Installation

INSTALL ( FILES 
  ${CMAKE_CURRENT_BINARY_DIR}/config/IlmBaseConfig.h
  DESTINATION
  include/OpenEXR

)

if(ILMIMF_INSTALL_PKGCONFIG)
FILE ( WRITE ${CMAKE_BINARY_DIR}/IlmBase.pc "prefix=${CMAKE_INSTALL_PREFIX}\n" )
FILE ( APPEND ${CMAKE_BINARY_DIR}/IlmBase.pc "exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include
OpenEXR_includedir=\${prefix}/include/OpenEXR

Name: IlmBase
Description: Base math and exception libraries
Version: ${ILMBASE_VERSION}
Libs: -L\${libdir} -lImath${ILMBASE_LIBSUFFIX} -lIexMath${ILMBASE_LIBSUFFIX} -lHalf -lIex${ILMBASE_LIBSUFFIX} -lIlmThread${ILMBASE_LIBSUFFIX} -pthread
Cflags: -pthread -I\${OpenEXR_includedir}
")

INSTALL ( FILES
  ${CMAKE_BINARY_DIR}/IlmBase.pc
  DESTINATION
  lib/pkgconfig
)
endif()
